﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;

namespace zichan.BLL
{
    public class AssetLogService
    {
        public static void AddLog(string operation, int assetId, int staff)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                Asset asset = AssetService.Get(assetId);
                zichan.Entity.AsssetLog model = new AsssetLog();
                model.AssetId = assetId;
                model.Code = asset.Code;
                model.StaffId = staff;
                model.Operation = operation;
                model.Time = DateTime.Now;
                db.AsssetLogs.Add(model);
                db.SaveChanges();
            }
        }
        public static IPagedList<AsssetLog> List(string code, string name, string staff, string keyWord, DateTime? sdt, DateTime? edt, int pageIndex, int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<AsssetLog> log = db.AsssetLogs;
                if (!string.IsNullOrEmpty(code))
                    log = log.Where(o => o.Code.Contains(code));
                if (!string.IsNullOrEmpty(name))
                {
                    List<int> ids = db.Assets.Where(o => o.Name.Contains(name)).Select(o => o.Id).ToList();
                    log = log.Where(o => ids.Contains(o.AssetId));
                }
                if (!string.IsNullOrEmpty(staff))
                {
                    List<int> ids = db.Staffs.Where(o => o.Name.Contains(staff)).Select(o => o.Id).ToList();
                    log = log.Where(o => ids.Contains(o.StaffId));
                }
                if (!string.IsNullOrEmpty(keyWord))
                {
                    List<int> ids = db.Assets.Where(o => o.DistributionRemark.Contains(keyWord) || o.RecoveryRemark.Contains(keyWord) || o.ScrapRemark.Contains(keyWord)).Select(o => o.Id).ToList();
                    log = log.Where(o => o.Operation.Contains(keyWord) || ids.Contains(o.AssetId));
                }
                if (sdt.HasValue)
                {
                    log = log.Where(o => o.Time >= sdt);
                }
                if (edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}", edt));
                    log = log.Where(o => o.Time <= edt);
                }
                List<AsssetLog> list = log.OrderByDescending(o => o.Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                List<int> assetIds = list.Select(o => o.AssetId).ToList();
                var assetNames = db.Assets.Where(o => assetIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> staffIds = list.Select(o => o.StaffId).ToList();
                var staffNames = db.Staffs.Where(o => staffIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                foreach(AsssetLog a in list)
                {
                    a.AssetName = assetNames.FirstOrDefault(o => o.Id == a.AssetId).Name;
                    a.StaffName = staffNames.FirstOrDefault(o => o.Id == a.StaffId).Name;
                }
                return list.OrderByDescending(o => o.Time).ToPagedList(pageIndex - 1, pageSize, log.Count());
            }
        }
    }
}
